﻿using System.Collections.Generic;
using System.Text;
using System.Xml;
using Jayden.Dll.Sc2.Descriptions;
using Jayden.Sc2;

namespace Jayden.Dll.Sc2
{
	public class ActionList : List<ActionDescription>
	{
		public ActionList() { }
		public ActionList(XmlDocument doc, RaceDictionary races)
		{
			XmlAttribute race = doc.DocumentElement.Attributes["race"];
			if (race == null)
				return;
			RaceDescription description = races[race.Value];
			foreach (XmlNode node in doc.DocumentElement.ChildNodes)
				if (node is XmlElement)
					Add(description.GetAction(node.Attributes["id"].Value));
		}
		public ActionList(IEnumerable<ActionDescription> actions) : base(actions) { }
		public ActionList(RaceDescription race, IEnumerable<string> actions)
		{
			foreach (string id in actions)
			{
				ActionDescription action = race.GetAction(id);
				Add(action);
			}
		}

		public XmlDocument XmlDocument
		{
			get
			{
				XmlDocument document = new XmlDocument();
				XmlElement actionList = document.CreateElement("ActionList");
				document.AppendChild(actionList);
				if (Count > 0)
				{
					XmlAttribute version = document.CreateAttribute("version");
					XmlAttribute race = document.CreateAttribute("race");
					version.Value = this[0].Entity.Race.RaceDictionary.Version;
					race.Value = this[0].Entity.Race.Id;
					actionList.Attributes.Append(version);
					actionList.Attributes.Append(race);
					foreach (ActionDescription action in this)
					{
						XmlElement actionNode = document.CreateElement("Action");
						actionList.AppendChild(actionNode);
						XmlAttribute id = document.CreateAttribute("id");
						actionNode.Attributes.Append(id);
						id.Value = action.Id;
					}
				}
				return document;
			}
		}
		public string PlainText
		{
			get
			{
				StringBuilder builder = new StringBuilder();
				foreach (ActionDescription action in this)
					builder.AppendLine(action.Id);
				return builder.ToString();
			}
		}
	}
}
